package com.neusoft.databus.agent.pool.menjin;

import org.apache.commons.pool2.PooledObject;
import org.apache.http.impl.client.CloseableHttpClient;
import org.springframework.stereotype.Component;

import com.neusoft.databus.agent.utils.HttpClientUtil;
import com.neusoft.databus.common.connection.MenjinConnection;
import com.neusoft.databus.common.pool.AbstractPoolFactory;

/**
 * description
 *
 * @author sunchf
 * @date 2019年4月2日 上午9:28:26
 */
@Component
public class MenjinFactory extends AbstractPoolFactory<MenjinConnection, CloseableHttpClient> {

    @Override
    public CloseableHttpClient create(final MenjinConnection menjinConnection) throws Exception {

        HttpClientUtil.initialize(menjinConnection.getHost(), menjinConnection.getPort());
        final CloseableHttpClient client = HttpClientUtil.getHttpClient();
        return client;
    }

    @Override
    public void destroy(CloseableHttpClient v) {
        try {
            v.close();
        } catch (final Exception e) {
        }
        v = null;
    }

    @Override
    public boolean validateObject(final MenjinConnection key, final PooledObject<CloseableHttpClient> p) {
        final boolean validated = true;
        return validated;
    }

}
